package com.eco.mam.work.request.transform;

import java.io.StringWriter;
import java.util.ArrayList;

import javanet.staxutils.IndentingXMLStreamWriter;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.log4j.Logger;

import com.eco.mam.work.DataException;
import com.eco.mam.work.request.WorkRequestProcessor;

public class XMLTransformUtils {

    static final Logger logger = Logger.getLogger(XMLTransformUtils.class);

    // This method creates an XMLStreamWriter and writes xml elements to a
    // StrinWriter
    public StringWriter createXMLDocumentAsStringWriterFromCsvTokens(
            ArrayList<String[]> strCsvTokensList) throws DataException {
        StringWriter stringWriter = null;
        XMLStreamWriter writer = null;
        XMLOutputFactory outputFactory = null;
        String strCsvTokens[] = null;
        stringWriter = new StringWriter();
        outputFactory = XMLOutputFactory.newInstance();
        try {
            writer = new IndentingXMLStreamWriter(
                    outputFactory.createXMLStreamWriter(stringWriter));
            writer.writeStartDocument("1.0");
            writer.writeStartElement("Work_Request_Details");
            writer.writeAttribute("suppliercode", "ECO");
            writer.writeStartElement("Work_Requests");
            writer.writeAttribute(" total_no_of_work_requests",
                    String.valueOf(strCsvTokensList.size()));
            for (int i = 0; i < strCsvTokensList.size(); i++) {
                strCsvTokens = strCsvTokensList.get(i);
                for (int j = 0; j < strCsvTokens.length; j++) {
                    try {
                        switch (j) {
                        case 0:
                            writer.writeStartElement("Work_Request");
                            writer.writeAttribute("request_no",
                                    String.valueOf(i + 1));
                            writer.writeStartElement("Header");
                            writer.writeStartElement("Comment");
                            writer.writeCharacters("new installation");
                            writer.writeEndElement();
                            writer.writeStartElement("Priority");
                            writer.writeCharacters("1");
                            writer.writeEndElement();
                            writer.writeEndElement();
                            writer.writeStartElement("Transaction");
                            writer.writeStartElement("Record_Identifier");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            break;
                        case 1:
                            writer.writeStartElement("Transaction_Reference");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            break;
                        case 2:
                            writer.writeStartElement("Contract_Reference");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            break;
                        case 3:
                            writer.writeStartElement("Transaction_TypeCode");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            break;
                        case 4:
                            if (!strCsvTokens[j].equals(" ")) {
                                writer.writeStartElement("Transaction_Status_Code");
                                writer.writeCharacters(strCsvTokens[j]);
                                writer.writeEndElement();
                            }
                            break;
                        case 5:
                            writer.writeStartElement("Meter_Point");
                            writer.writeStartElement("MTPT_Record_Identifier");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            break;
                        case 6:
                            writer.writeStartElement("MeterPoint_Referene_Number");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            break;
                        case 7:
                            if (!strCsvTokens[j].equals(" ")) {
                            writer.writeStartElement("Location_Code");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            }
                            break;
                        case 8:
                            writer.writeStartElement("Address");
                            writer.writeStartElement("Address_Type_Code");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            break;
                        case 9:
                            writer.writeStartElement("PostCode");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            break;
                        case 10:
                            if (!strCsvTokens[j].equals(" ")) {
                            writer.writeStartElement("PostTown");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            }
                            break;
                        case 11:
                            if (!strCsvTokens[j].equals(" ")) {
                            writer.writeStartElement("County");
                            writer.writeCharacters(strCsvTokens[j]);
                            writer.writeEndElement();
                            }
                            writer.writeEndElement();
                            writer.writeEndElement();
                            writer.writeEndElement();
                            writer.writeEndElement();
                            break;
                        default:
                            break;
                        }
                    } catch (XMLStreamException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if (j == strCsvTokens.length - 1) {
                        break;
                    }
                }
            }
            writer.writeEndElement();
            writer.writeEndElement();
            writer.writeEndDocument();
        } catch (XMLStreamException e) {
            throw new DataException(
                    "Error happened, csv data can not  be transformed to xml",
                    e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("before returning  from :createXMLDocumentAsStringWriterFromCsvTokens()  :"
                    + writer);
          /*  logger.debug("before returning  from :createXMLDocumentAsStringWriterFromCsvTokens()  :"
                    + stringWriter);*/
        }
        return stringWriter;
    }

}
